/*
 * @Description: 
 * @Date: 2020-12-07 11:02:58
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2021-01-27 09:57:53
 * @FilePath: \AccountOpening-H5\vue.config.js
 */
const path = require("path");
const resolve = dir => require('path').join(__dirname, dir);
const webpack = require("webpack");
const CompressionPlugin = require("compression-webpack-plugin")
const cdnDependencies = require('./cdn')
const externals = {}
cdnDependencies.forEach(pkg => { externals[pkg.name] = pkg.library })
// 引入文件的 cdn 链接
const cdn = {
    css: cdnDependencies.map(e => e.css).filter(e => e),
    js: cdnDependencies.map(e => e.js).filter(e => e)
}
module.exports = {
  lintOnSave: false,
  publicPath: "",
  assetsDir: "",
  integrity: true,
  outputDir: 'dist',
  productionSourceMap: false,
  pwa:{
    name:"开户系统",
    assetsVersion:'1.0',
  },

  chainWebpack: config => {
    config.entry('polyfill').add('@babel/polyfill')
    // markdown
    config.module
      .rule('md')
      .test(/\.md$/)
      .use('text-loader')
      .loader('text-loader')
      .end()
    if(process.env.NODE_ENV === 'production'){
        config.plugin('html')
        .tap(args => {
            args[0].cdn = cdn;
          return args;
        })
      }
    config
      .output
      .filename('js/lother.[hash:3].js')

    
  },
  configureWebpack: config => {
    var baseConfig = {
      name: 'name',
      resolve: {
        alias: {
          '@': resolve('src')
        }
      }
    }
    const configNew = {}
    if (process.env.NODE_ENV === 'production') {
      configNew.externals = externals
      configNew.plugins = [
        // gzip
        new CompressionPlugin({
          test: /\.js$|\.css/,
          threshold: 30720,//超出30K压缩
          minRatio: 0.8,
          deleteOriginalAssets: false
        })
      ]
    }
    if (process.env.NODE_ENV === 'production') {
      return {
        plugins: [
          new webpack.ProvidePlugin({
            $: 'jquery',
            jQuery: 'jquery',
            'window.jQuery': 'jquery',
          }),
          new CompressionPlugin({
            test:/\.js$|\.html$|.\css/, //匹配文件名
            threshold: 10240,//对超过10k的数据压缩
            deleteOriginalAssets: false //不删除源文件
          }),
        ],
        ...baseConfig,
        ...configNew
      }
     
    } else {
      return {
        plugins: [
          new webpack.ProvidePlugin({
            $: 'jquery',
            jQuery: 'jquery',
            'window.jQuery': 'jquery',
          }),
        ],
        ...baseConfig,
        ...configNew
      }
    }
  },
  pluginOptions: {
    'style-resources-loader': {
      preProcessor: 'less',
      patterns: [
        path.resolve(__dirname, './src/assets/less/global.less')
      ]
    }
  },
  css: {
    requireModuleExtension: true,
    // 是否使用 css 分离插件 ExtractTextPlugin，采用独立样式文件载入，不采用 <style> 方式内联至 html 文件中
    extract: false,//
    sourceMap: false,
  },
  devServer: {
    port: 369,
    host: '100.0.1.27',
    // proxy: {
    //   '/lother': {
    //     target: 'http://hwx.usa.linknet.ltd/',  // target host
    //     ws: true,  // proxy websockets 
    //     changeOrigin: true,  // needed for virtual hosted sites
    //     pathRewrite: {
    //       '^/lother': '/'
    //     }
    //   }
    // }
  }
}
